//
//  BottomView.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/7.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

protocol BottomViewDelegate: NSObjectProtocol {
    func selectAllAction(sender: UIButton)
}

class BottomView: UIView {
    
    var delegate: BottomViewDelegate!
    
    // MARK: - life cycle
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initBaseLayout()
        layoutPageSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - event response
    func selectAllAction(sender: UIButton) {
        if self.delegate.responds(to: #selector(BottomView.selectAllAction(sender:))) {
            self.delegate.selectAllAction(sender: sender)
        }
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.addSubview(backView)
        self.addSubview(selectAllBtn)
        self.addSubview(selectAllLabel)
        self.addSubview(totalLabel)
        self.addSubview(freightLabel)
        self.addSubview(accountBtn)
        self.addSubview(collectionBtn)
        self.addSubview(deletBtn)
    }
    
    func layoutPageSubviews() {
        backView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(self).offset(0)
            make.width.equalTo(SCREEN_WIDTH/3*2)
            make.bottom.equalTo(self).offset(0)
        }
        selectAllBtn.snp.makeConstraints { (make) in
            make.left.equalTo(backView).offset(5)
            make.centerY.equalTo(backView)
            make.width.height.equalTo(32)
        }
        
        selectAllLabel.snp.makeConstraints { (make) in
            make.left.equalTo(selectAllBtn.snp.right).offset(1)
            make.centerY.equalTo(backView)
            make.width.equalTo(50)
            make.height.equalTo(14)
        }
        
        totalLabel.snp.makeConstraints { (make) in
            make.right.equalTo(backView).offset(-14)
            make.top.equalTo(backView).offset(14)
            make.width.equalTo(113)
            make.height.equalTo(12)
        }
        
        freightLabel.snp.makeConstraints { (make) in
            make.right.equalTo(backView).offset(-14)
            make.top.equalTo(totalLabel.snp.bottom).offset(2)
            make.width.equalTo(50)
            make.height.equalTo(12)
        }
        
        accountBtn.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(0)
            make.top.equalTo(self).offset(0)
            make.bottom.equalTo(self).offset(0)
            make.width.equalTo(SCREEN_WIDTH/3)
        }
        
        deletBtn.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-14)
            make.centerY.equalTo(self)
            make.width.equalTo(60)
            make.height.equalTo(32)
        }
        
        collectionBtn.snp.makeConstraints { (make) in
            make.right.equalTo(deletBtn.snp.left).offset(-14)
            make.centerY.equalTo(self)
            make.width.equalTo(84)
            make.height.equalTo(32)
        }
        
    }
    
    func configUI() {
        totalLabel.text = "合计：" + "1029.00"
        freightLabel.text = "未包含运费"
        accountBtn.setTitle("去结算" + "(3)", for: .normal)
    }
    
    // MARK: - setters and getters
    var _isEdit: Bool?
    var isEdit: Bool? {
        set {
            if !newValue! {
                collectionBtn.isHidden = true
                deletBtn.isHidden = true
                backView.isHidden = false
                totalLabel.isHidden = false
                freightLabel.isHidden = false
                accountBtn.isHidden = false
            } else {
                backView.isHidden = true
                totalLabel.isHidden = true
                freightLabel.isHidden = true
                accountBtn.isHidden = true
                collectionBtn.isHidden = false
                deletBtn.isHidden = false
            }
        } get {
            return _isEdit
        }
    }
    var _backView: UIView!
    var backView: UIView {
        if _backView == nil {
            _backView = UIView()
        }
        return _backView
    }
    var _selectAllBtn: UIButton!
    var selectAllBtn: UIButton {
        if _selectAllBtn == nil {
            _selectAllBtn = UIButton()
            _selectAllBtn.setBackgroundImage(UIImage(named: "icon_select_n"), for: .normal)
            _selectAllBtn.setBackgroundImage(UIImage(named: "icon_select_h"), for: .highlighted)
            _selectAllBtn.setBackgroundImage(UIImage(named: "icon_select_s"), for: .selected)
            _selectAllBtn.addTarget(self, action: #selector(BottomView.selectAllAction(sender:)), for: .touchUpInside)
        }
        return _selectAllBtn
    }
    
    var _selectAllLabel: UILabel!
    var selectAllLabel: UILabel {
        if _selectAllLabel == nil {
            _selectAllLabel = UILabel()
            _selectAllLabel.font = UIFont(name: "PingFang-SC-Regular", size: 14)
            _selectAllLabel.textColor = UIColor(hexString: "#252525")
            _selectAllLabel.text = "全选"
        }
        return _selectAllLabel
    }
    
    var _totalLabel: UILabel!
    var totalLabel: UILabel {
        if _totalLabel == nil {
            _totalLabel = UILabel()
            _totalLabel.font = UIFont(name: "PingFang-SC-Regular", size: 14)
            _totalLabel.textColor = UIColor(hexString: "#252525")
            _totalLabel.textAlignment = .right
        }
        return _totalLabel
    }
    
    var _freightLabel: UILabel!
    var freightLabel: UILabel {
        if _freightLabel == nil {
            _freightLabel = UILabel()
            _freightLabel.font = UIFont(name: "PingFang-SC-Regular", size: 10)
            _freightLabel.textColor = UIColor(hexString: "#999999")
            _freightLabel.textAlignment = .right
        }
        return _freightLabel
    }
    
    var _accountBtn: UIButton!
    var accountBtn: UIButton {
        if _accountBtn == nil {
            _accountBtn = UIButton()
            _accountBtn.backgroundColor = UIColor(hexString: "#B50834")
            _accountBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        }
        return _accountBtn
    }
    
    var _collectionBtn: UIButton!
    var collectionBtn: UIButton {
        if _collectionBtn == nil {
            _collectionBtn = UIButton()
            _collectionBtn.backgroundColor = UIColor.white
            _collectionBtn.layer.borderWidth = 0.5
            _collectionBtn.layer.borderColor = UIColor(hexString: "#B7B7B7")?.cgColor
            _collectionBtn.setTitle("移入收藏", for: .normal)
            _collectionBtn.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 12)
            _collectionBtn.setTitleColor(UIColor(hexString: "#252525"), for: .normal)
            
        }
        return _collectionBtn
    }
    
    var _deletBtn: UIButton!
    var deletBtn: UIButton {
        if _deletBtn == nil {
            _deletBtn = UIButton()
            _deletBtn.backgroundColor = UIColor(hexString: "#B50834")
            _deletBtn.setTitle("删除", for: .normal)
            _deletBtn.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
            _deletBtn.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 12)
        }
        return _deletBtn
    }
    
}
